package item.pickable;

import item.*;
import attribute.*;
import be.kuleuven.cs.som.annotate.Basic;
import java.util.*;

/**
 * @invar A surprise box cannot both be owned by a robot and be located
 *				on a board at the same time.
 *			|!(this.isOnABoard && this.isOnARobot)
 *
 * @invar A surprise box must at all time have a valid weight, unless the repair kit is terminated.
 * 			|if(!this.isTerminated())
 * 			|	this.isValidWeight(surprise box.getWeight())
 */
public class SurpriseBox extends PickableItem{
	private int SurpriseBoxContent;
	private static Random random = new Random();
	
	/**
	 * Initialises this new SurpriseBox with the given X and Y coordinate, the given weight 
	 * 		and the given board it has to be placed on.
	 * 
	 * @param X
	 * 		The X coordinate of the surprise box's new position on the given board.
	 * @param Y
	 * 		The Y coordinate of the surprise box's new position on the given board.
	 * @param board
	 * 		The board the surprise box must be placed on.
	 * @param weight
	 * 		The weight the surprise box must have.
	 * 
	 * @effect The new weight of the surprise box is set to the given weight using setWeight().
	 * 			|This.setWeight(weight)
	 * 
	 * @effect The position and board of the surprise box is set using setValidPosition(X, Y, board)
	 * 			| this.setValidPosition(X, Y, board)
	 */
	public SurpriseBox(long X, long Y, int weight, Board board) {
		this.setWeight(weight);
		this.setValidPosition(X, Y, board);
	}

	/**
	 * Initialise a new SurpriseBox as a surprise box with -1, -1 and null as its position and board.
	 * 
	 * @effect	This new surprise box is initialised with the given weight.
	 * 			|this.setValidWeight()
	 */
	public SurpriseBox(int weight) {
		this.setWeight(weight);
	}
	
	/**
	 * Initialise a new SurpriseBox as a surprise box with -1, -1 and null as its position and board.
	 * 
	 * @effect	This new surprise box is initialised with randomly weight.
	 */
	public SurpriseBox(){
		this.setWeight((random.nextInt(getMaxWeight()/100) + 1) * 100);
	}



	/**
	 * @return the surpriseBoxContent
	 */
	@Basic
	public int getSurpriseBoxContent() {
		return SurpriseBoxContent;
	}

	/**
	 * @param surpriseBoxContent	the surpriseBoxContent to set
	 */
	public void setSurpriseBoxContent(int surpriseBoxContent) {
		SurpriseBoxContent = surpriseBoxContent;
	}

	/**
	 * This surpriseBox hits every adjacent position.
	 * 
	 * @post ...
	 * 			|for each adjacent position:
	 * 			|	if(position != null)
	 * 			|		each item in position.getListOfItemsOnPosition:
	 * 			|			if(item ==this) this.terminate()
	 * 			|			else	item.hitByLaser()
	 */
	@Override
	public void hitByLaser() {
		for(long x = this.getPosition().getX()-1 ; x == this.getPosition().getX() +1; x++){
			for(long y = this.getPosition().getY()-1; y == this.getPosition().getY()+1; y++ ){
				Position p = this.doesPositionAlreadyExist(x, y, this.getPosition().getBoard());
				if(p != null)
					for(Item item : p.getListOfItemsAtPosition())
						if(item != this)	item.hitByLaser();
				}
		}
		this.terminate();
	}
	
	@Override
	public boolean hasAtleast(Object condition){
		if(condition.getClass() == int.class){
			if(this.getWeight() >= (Integer)condition)
				return true;
		}
		else if(condition.getClass() == Position.class){
			if((this.getPosition() == ((Position)condition)))
				return true;
		}
		return false;	
	}
}